/**
 * 
 */
package com.barisergun.memcached;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author barise
 * @date Dec 17, 2010
 * 
 */
public class MemcachedProcess {

	private List<Process> memCachedProcessList;

	public boolean start (List<List<String>> listOfProcessArguments) throws IOException {

		memCachedProcessList = new LinkedList<Process> ();

		for (List<String> processArguments : listOfProcessArguments) {

			ProcessBuilder pb = new ProcessBuilder (processArguments);			

			memCachedProcessList.add (pb.start ());
		}

		boolean allProcessesStarted = true;

		for (Process memcached : memCachedProcessList) {
			if (memcached == null) {
				allProcessesStarted = false;
				break;

			}
		}

		return allProcessesStarted;

	}

	public void stop () throws InterruptedException {

		if (memCachedProcessList != null) {

			for (Process memcached : memCachedProcessList) {
				memcached.destroy ();
				memcached.waitFor ();
			}

		}

	}

}
